package model.workersWithData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;

import model.workersWithData.sort.SortOrderByDeadlineDate;

import org.apache.log4j.Logger;

import api.entitites.IGarage;
import api.entitites.IMaster;
import api.entitites.IOrder;
import api.entitites.OrderStatus;
import api.messages.Messager;

public class WorkerWithOrder {

	private static SimpleDateFormat format = new SimpleDateFormat(Messager.DATE_FORMAT);
	private static final Logger log = Logger.getLogger(WorkerWithOrder.class);
	private static ArrayList<String> arrayMessages= new ArrayList<String>();

	
	public static ArrayList<String> getMastersByOrder(Integer numberOrder,
			ArrayList<IOrder> ordList) {
		
		arrayMessages.clear();
		try {
			IMaster master=null;
			for (IOrder o : ordList) {
				if (o.getNumberOrder() == numberOrder){
					master=o.getMaster();
				}
			}
			
			if (master != null) {
				String tempMess=Messager.MASTER+master.getNameMaster()+Messager.SERVICE+numberOrder;
				arrayMessages.add(tempMess);
			} else {
				arrayMessages.add(Messager.ORD_NOT_EXISTS);
			}
		} catch (NullPointerException ne) {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
			
		}
		return arrayMessages;
	}

	
	public static ArrayList<String> getListMadingOrder(ArrayList<IOrder> orderList,
			Comparator sortby) {
		arrayMessages.clear();
		try {
			ArrayList<IOrder> ordList = new ArrayList<IOrder>();
			Date date = new Date();
			for (IOrder ord : orderList) {
				if (ord.getBeginingDate().before(date)
						&& ord.getDeadlineDate().after(date)
						&& ord.getOrderStatus() == null) {
					ordList.add(ord);
				}
			}
			Collections.sort(ordList, sortby);
			
			for (IOrder o:ordList){
				arrayMessages.add(Messager.ORDER+o.getNumberOrder());
			}	
			
		} catch (NullPointerException ne) {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
		}
		return arrayMessages;

	}


	public static ArrayList<String> SortOrder(ArrayList<IOrder> orderList, Comparator sortBy) {
		arrayMessages.clear();
		if(!orderList.isEmpty()){
			Collections.sort(orderList, sortBy);
			for (IOrder o:orderList){
				arrayMessages.add(Messager.ORDER+o.getNumberOrder());
			}	
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
		}
		return arrayMessages;
	}
	

	public static ArrayList<String> getNextFreeDate(ArrayList<IOrder> orderList) {
		arrayMessages.clear();
		if(!orderList.isEmpty()){
			Collections.sort(orderList, new SortOrderByDeadlineDate());
			GregorianCalendar date=new GregorianCalendar();
			Date temp = orderList.get(0).getDeadlineDate();
			date.setTime(temp);
			date.set(Calendar.DAY_OF_YEAR, 1);
			temp=date.getTime();			
			arrayMessages.add(Messager.NEXT_FREE_DATE+format.format(temp.getTime()));
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
			throw new RuntimeException(Messager.NOT_ORDER);
		}
		return arrayMessages;
	}

	
	public static ArrayList<String> getOrderByPeriod(ArrayList<IOrder> orderList,
			Comparator sortby, OrderStatus ordSt, Date begin,
			Date end) {
		arrayMessages.clear();
		if(!orderList.isEmpty()) {
			ArrayList<IOrder> ordList = new ArrayList<IOrder>();

			for (IOrder ord : orderList) {
				if ((ord.getBeginingDate().after(begin) && ord
						.getBeginingDate().before(end))
						&& ord.getOrderStatus() == ordSt) {
					ordList.add(ord);
				}
			}
			if (ordList.isEmpty()) {
				arrayMessages.add(Messager.ORD_STAT_NOT_FOUND);
			} else {
				Collections.sort(ordList, sortby);
				for (IOrder o:ordList){
					arrayMessages.add(Messager.ORDER+o.getNumberOrder());
				}	
			}
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
		}
		return arrayMessages;
	}

	
	public static ArrayList<String> addOrder(ArrayList<IOrder> orderList, IOrder order,
			ArrayList<IMaster> masList, ArrayList<IGarage> garList,
			String masterName, Integer numberGarage) {
		arrayMessages.clear();

		for (IOrder o : orderList) {
			if (o.getNumberOrder() == order.getNumberOrder()) {
				order.setNumberOrder(orderList.size() + 1);
				arrayMessages.add(Messager.ORD_EXISTS + order.getNumberOrder());

			}
		}
		orderList.add(order);
		arrayMessages.add(Messager.ORDER_ADD);
		
		IMaster master = null;
		IGarage garage = null;
		for (IMaster m : masList) {
			if (m.getNameMaster().equals(masterName)) {
				master = m;
			}
		}

		for (IGarage g : garList) {
			if (g.getNumberGarage() == numberGarage) {
				garage = g;
			}
		}
		order.sendMasterGarage(master, garage);
		return arrayMessages;

	}

	public static ArrayList<String> markOrder(ArrayList<IOrder> orderList,
			OrderStatus ordSt, Integer orderNumber) {
		arrayMessages.clear();
		if(!orderList.isEmpty()) {
			int i = 0;
			for (IOrder o : orderList) {
				if (o.getNumberOrder() == orderNumber) {
					o.setOrderStatus(ordSt);
					o.getMaster().markMasterFree();
					o.getGarage().markGarageEmpty();
					i++;
					arrayMessages.add(Messager.ORDER_STATUS_CHANGE);
				}
			}
			if (i == 0) {
				arrayMessages.add(Messager.ORD_NOT_EXISTS);
			} 
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
		}
		return arrayMessages;
	}

	
	public static ArrayList<String> ShirtTimeOrder(ArrayList<IOrder> orderList,
			Date newDate, Integer numberOrder) {
		arrayMessages.clear();
		if(!orderList.isEmpty()){
			int i=0;
			for (IOrder ord : orderList) {
				if (ord.getNumberOrder() == numberOrder) {
					ord.shiftTime(newDate);
					i++;
					arrayMessages.add(Messager.ORDER_DATE_CHANGE);
				}
			}
			if(i==0){
				arrayMessages.add(Messager.ORD_NOT_EXISTS);
			}
		} else {
			log.warn(Messager.EMPTY_COLLEC);
			arrayMessages.add(Messager.NOT_ORDER);
		}
		return arrayMessages;
	}

	
	public static ArrayList<String> cloneOrder(ArrayList<IOrder> orderList, IOrder order,
			ArrayList<IMaster> masList, ArrayList<IGarage> garList) {
		arrayMessages.clear();
		
		try {
			IOrder clone = order.cloneOrder();
			addOrder(orderList,clone,masList,garList,clone.getMaster().getNameMaster(), clone.getGarage().getNumberGarage());
			arrayMessages.add(Messager.CLONE);
		} catch (CloneNotSupportedException e) {
			log.error(Messager.EXCEPTION_CLONE);
			arrayMessages.add(Messager.EXCEPTION_CLONE);
		}
		return arrayMessages;
	}

}
